//
//  LCZSpecialDetailViewController.m
//  PersonalNews
//
//  Created by dllo on 15/12/2.
//  Copyright © 2015年 changshuhua. All rights reserved.
//

#import "LCZSpecialDetailViewController.h"
#import "LCZSpecialModel.h"
#import "LCZTopicsModel.h"
#import "LCZDocsModel.h"
#import "LCZImagextraModel.h"
#import "SpecialDetailCollectionCell.h"
#import "LCZNormalTableViewCell.h"
#import "LCZPhotosetTableViewCell.h"
#import "LCZNormalDetailViewController.h"
#import "LCZPicDetailViewController.h"

@interface LCZSpecialDetailViewController ()<UITableViewDelegate, UITableViewDataSource, UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, strong) LCZBaseCollectionView *collectionView;
@property(nonatomic, strong) LCZBaseTableView *tableView;
@property(nonatomic, strong) LCZBaseView *headView;
@property(nonatomic, strong) LCZBaseLabel *detailLabel;
@property(nonatomic, strong) LCZBaseImageView *imageView;
@property(nonatomic, strong) NSMutableArray *array;
@property(nonatomic, strong) LCZSpecialModel *specialModel;


@end

@implementation LCZSpecialDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self createView];
    [self createDate];
}

- (void)createView {
    self.tableView = [[LCZBaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 30) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.headView = [[LCZBaseView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 200)];
    self.tableView.tableHeaderView = self.headView;
//    self.headView.backgroundColor = [UIColor yellowColor];
    
    self.imageView = [[LCZBaseImageView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 70)];
    [self.headView addSubview:self.imageView];
//    self.imageView.backgroundColor = [UIColor magentaColor];
    
    
    self.detailLabel = [[LCZBaseLabel alloc] initWithFrame:CGRectMake(0, 70, SCREENWIDTH, 50)];
    [self.headView addSubview:self.detailLabel];
//    self.detailLabel.backgroundColor = [UIColor grayColor];
    self.detailLabel.numberOfLines = 0;

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SCREENWIDTH / 5, 30);
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 0, 5);
    
    self.collectionView = [[LCZBaseCollectionView alloc] initWithFrame:CGRectMake(0, 120, SCREENWIDTH, 80) collectionViewLayout:layout];
    [self.headView addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.collectionView registerClass:[SpecialDetailCollectionCell class] forCellWithReuseIdentifier:@"collection"];
}
#pragma mark -- collectionView协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SpecialDetailCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath];
    
    cell.layer.borderWidth = 1;
//    cell.backgroundColor = [UIColor whiteColor];
    cell.label.text = [self.array[indexPath.row] shortname];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.row] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

#pragma mark --tableView协议方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([[[self.array[indexPath.section] docs][indexPath.row] skipType] isEqualToString:@"photoset"]) {
        static NSString *reuse = @"photoset";
        LCZPhotosetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[LCZPhotosetTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }
        
        cell.titleLabel.text = [[self.array[indexPath.section] docs][indexPath.row] title];
        [cell.leftImg sd_setImageWithURL:[NSURL URLWithString:[[self.array[indexPath.section] docs][indexPath.row] imgsrc]] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
        [cell.midImg sd_setImageWithURL:[NSURL URLWithString:[[[self.array[indexPath.section] docs][indexPath.row] imgextra][0] imgsrc]] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
        [cell.rightImg sd_setImageWithURL:[NSURL URLWithString:[[[self.array[indexPath.section] docs][indexPath.row] imgextra][1] imgsrc]] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
         cell.replyCountLabel.text = [NSString stringWithFormat:@"%@跟帖", [[self.array[indexPath.section] docs][indexPath.row] replyCount]];
        
        return cell;

    } else {
        static NSString *reuse = @"reuse";
        LCZNormalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[LCZNormalTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }
        [cell.imgsrcView sd_setImageWithURL:[NSURL URLWithString:[[self.array[indexPath.section] docs] [indexPath.row] imgsrc]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        cell.titleLabel.text = [[self.array[indexPath.section] docs][indexPath.row] title];
        cell.digestLabel.text = [[self.array[indexPath.section] docs][indexPath.row] digest];
        cell.replyCountLabel.text = [NSString stringWithFormat:@"%@跟帖", [[self.array[indexPath.section] docs][indexPath.row] replyCount]];
        return cell;
        

    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([[[self.array[indexPath.section] docs][indexPath.row] skipType] isEqualToString:@"photoset"]) {
        return 120;
    } else {
        return 80;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    if (section == 0) {
//        return 30;
//    } else {
//        return 15;
//    }
    return 15;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.array.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [self.array[section] shortname];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.array[section] docs].count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LCZDocsModel *model = [self.array[indexPath.section] docs][indexPath.row];
    
    if ([model.skipType isEqualToString:@"photoset"]) {
        LCZPicDetailViewController *controller = [[LCZPicDetailViewController alloc] init];
        [self.navigationController pushViewController:controller animated:YES];
        controller.titleStr = model.title;
        NSString *str = model.photosetID;
        NSString *firstStr = [str substringFromIndex:9];
        NSString *secondStr = [str substringWithRange:NSMakeRange(4, 4)];
        NSString *strURL = [NSString stringWithFormat:@"http://c.3g.163.com/photo/api/set/%@/%@.json", secondStr, firstStr];
        controller.URLStr = strURL;


    } else {
        LCZNormalDetailViewController *controller = [[LCZNormalDetailViewController alloc] init];
        [self.navigationController pushViewController:controller animated:YES];
        controller.url = model.url;
    }
    

}

- (void)createDate {
    [ZLPNetWorkTool getWithURL:self.URLStr success:^(id responseObject) {
        
        NSDictionary *dic = responseObject;
        self.array = [NSMutableArray array];
        
        self.specialModel = [[LCZSpecialModel alloc] init];
        self.specialModel.topics = dic[self.specialID][@"topics"];
        self.specialModel.digest = dic[self.specialID][@"digest"];
        self.specialModel.banner = dic[self.specialID][@"banner"];
        self.specialModel.sname = dic[self.specialID][@"sname"];
        
        self.array = [LCZTopicsModel baseModelArr:self.specialModel.topics];
        for (NSInteger i = 0; i < self.array.count; i++) {
            NSMutableArray *mArr = [NSMutableArray array];
            mArr = [LCZDocsModel baseModelArr:[self.array[i] docs]];
            [self.array[i] setValue:mArr forKey:@"docs"];
            for (NSInteger j = 0; j < mArr.count; j++) {
                if ([mArr[j] imgextra].count != 0) {
                    [mArr[j] setValue:[LCZImagextraModel baseModelArr:[mArr[j] imgextra]] forKey:@"imgextra"];
                }
            }
        }
        
        self.title = [self.specialModel sname];
        
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.specialModel.banner]];
        NSString *str = self.specialModel.digest;
   
        if (str.length == 0) {
            self.detailLabel.text = [self.specialModel sname];
        } else {
            self.detailLabel.text = str;
        }
        
        [self.tableView reloadData];
        [self.collectionView reloadData];

        
        
    } failure:^(NSError *error) {
       
        NSLog(@"%@", error);
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
